import 'package:flutter/material.dart';
import 'package:ichinesereader/models/common/ReadRanking.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class LeaderBoardDataGrids extends StatelessWidget {
  LeaderBoardDataGrids(this.myReadRankings);

  List<ReadRanking> myReadRankings;
  bool isWebOrDesktop =
      GetPlatform.isMacOS || GetPlatform.isWeb || GetPlatform.isDesktop;
  List<DataGridRow> dataGridRows = <DataGridRow>[];
  DataGridController _dataGridController = DataGridController();

  @override
  Widget build(BuildContext context) {
    Log.debug('build LeaderBoardDataGrids');
    return _buildDataGrid();
  }

  SfDataGrid _buildDataGrid() {
    return SfDataGrid(
      source: LeaderBoardDataGridSource(items: myReadRankings),
      onQueryRowHeight: (row) {
        return row.rowIndex == 0 ? 50.0 : 49.0;
      },
      //设置表头高度
      controller: _dataGridController,
      allowSorting: true,
      allowTriStateSorting: true,
      allowSwiping: false,
      selectionMode: SelectionMode.single,
      navigationMode: GridNavigationMode.row,
      columnWidthMode: ColumnWidthMode.auto,
      columnWidthCalculationRange: ColumnWidthCalculationRange.allRows,
      columns: <GridColumn>[
        GridColumn(
          width: isWebOrDesktop ? 200 : 150,
          columnName: 'Rank',
          label: buildDataGridColumnLabel('Rank'),
        ),
        GridColumn(
          columnName: 'Name',
          width: isWebOrDesktop ? 200 : 180,
          label: buildDataGridColumnLabel('Name'),
        ),
        GridColumn(
          columnName: 'TotalPoints',
          width: isWebOrDesktop ? 350.0 : 280.0,
          label: buildDataGridColumnLabel('TotalPoints'),
        ),
      ],
    );
  }
}

class LeaderBoardDataGridSource extends DataGridSource {
  LeaderBoardDataGridSource({required List<ReadRanking> items}) {
    dataGridRows = items
        .map<DataGridRow>((dataGridRow) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'Rank', value: dataGridRow.rank),
              DataGridCell<String>(
                  columnName: 'Name', value: dataGridRow.member),
              DataGridCell<double>(
                  columnName: 'TotalPoints', value: dataGridRow.score),
            ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    //列表隔行Row的颜色 // https://help.syncfusion.com/flutter/datagrid/conditional-styling
    Color setRowBackgroundColor() {
      final int index = effectiveRows.indexOf(row);
      if (index % 2 != 0) {
        return Get.isDarkMode ? Colors.black : Colors.grey[200]!;
      }
      return Colors.transparent;
    }

    return DataGridRowAdapter(
        color: setRowBackgroundColor(),
        cells: row.getCells().map<Widget>((dataGridCell) {
          return Container(
              //height: 18,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 16.0),
              child: Text(
                dataGridCell.value.toString(),
                //softWrap: true,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: Get.textTheme.bodySmall,
              ));
        }).toList());
  }
}
